package com.fwj.portal.service.business.product;

import com.alibaba.fastjson.JSONObject;
import com.fwj.common.model.AttachInfoVo;
import com.fwj.core.common.common.CommonUtils;
import com.fwj.core.common.constant.DataConstant;
import com.fwj.core.common.model.PageRequest;
import com.fwj.core.common.model.PageResponse;
import com.fwj.core.common.model.ResponseDTO;
import com.fwj.portal.api.business.product.BusinessProductService;
import com.fwj.portal.dao.business.product.BusinessProductInfoMapper;
import com.fwj.portal.entity.product.BusinessProductInfo;
import com.fwj.portal.model.business.product.BusinessProductInfoVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: fwj
 * @Data: 2022/5/5 14:09
 * @Version 1.0
 * @Description:    待优化
 */

@Service
public class BusinessProductServiceImpl implements BusinessProductService {

    // 导入日志
    private static final Logger LOGGER = LoggerFactory.getLogger(BusinessProductServiceImpl.class);

    @Autowired
    private BusinessProductInfoMapper businessProductInfoMapper;

    /**
     * Description: TODO 保存产品信息 => (发布、修改)
     * Author: fwj
     */
    @Override
    public ResponseDTO saveProduct(BusinessProductInfo businessProductInfo) {
        ResponseDTO responseDTO = new ResponseDTO();
        try {
            // 执行更新
            if (CommonUtils.isNotEmpty(businessProductInfo.getProductNumber())){
                updateBusinessProduct(businessProductInfo);
            }
            // 执行插入
            if (CommonUtils.isEmpty(businessProductInfo.getProductNumber())){
                insertBusinessProduct(businessProductInfo);
            }
            return responseDTO.success();
        } catch (Exception e) {
            LOGGER.error(e.toString());
            return responseDTO.fail();
        }
    }

    private void insertBusinessProduct(BusinessProductInfo businessProductInfo) throws Exception{
        BusinessProductInfo execBusinessProductInfo = buildBusinessProductInfo(businessProductInfo);
        businessProductInfoMapper.insertSelective(execBusinessProductInfo);
    }

    private BusinessProductInfo buildBusinessProductInfo(BusinessProductInfo businessProductInfo) throws Exception{
        BusinessProductInfo ret = JSONObject.parseObject(JSONObject.toJSONString(businessProductInfo),BusinessProductInfo.class);
        ret.setProductNumber(CommonUtils.generateUniqueNumber());
        LocalDateTime now = LocalDateTime.now();
        ret.setCreateTime(now);
        ret.setUpdateTime(now);
        ret.setStatus(DataConstant.NORMAL);
        return ret;
    }

    private void updateBusinessProduct(BusinessProductInfo businessProductInfo) throws Exception{
        BusinessProductInfo execBusinessProductInfo = new BusinessProductInfo();
        execBusinessProductInfo.setCreator(null);
        execBusinessProductInfo.setUpdateTime(LocalDateTime.now());
        Example example = Example.builder(BusinessProductInfo.class).build();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productNumber",execBusinessProductInfo.getProductNumber());
        criteria.andEqualTo("status",DataConstant.NORMAL);
        businessProductInfoMapper.updateByExampleSelective(execBusinessProductInfo,example);
    }

    /**
     * Description: TODO 删除产品信息
     * Author: fwj
     */
    @Override
    public ResponseDTO deleteProduct() {
        return null;
    }

    /**
     * Description: TODO 查询产品列表信息
     * Author: fwj
     */
    @Override
    public ResponseDTO queryProductList(PageRequest pageRequest) {
        ResponseDTO responseDTO = new ResponseDTO();
        try {
            PageHelper.startPage(pageRequest.getPage(), pageRequest.getPageSize());
            Example example = new Example(BusinessProductInfo.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andLessThan("releaseTime",LocalDateTime.now());
            criteria.andEqualTo("status",DataConstant.NORMAL);
            PageInfo<BusinessProductInfo> pageInfo = new PageInfo<>(businessProductInfoMapper.selectByExample(example));
            List<BusinessProductInfoVo> businessProductInfoVoList = JSONObject.parseArray(JSONObject.toJSONString(pageInfo.getList()),BusinessProductInfoVo.class);
            for (int i = 0; i < pageInfo.getList().size(); i++){
                businessProductInfoVoList.get(i).setAttachList(JSONObject.parseArray(pageInfo.getList().get(i).getAttachList(), AttachInfoVo.class));
            }
            PageResponse pageResponse = new PageResponse();
            pageResponse.setPageTotal(pageInfo.getTotal());
            pageResponse.setResponseBody(businessProductInfoVoList);
            return responseDTO.success().setData(pageResponse);
        } catch (Exception e) {
            LOGGER.error(e.toString());
            return responseDTO.fail();
        }
    }

    /**
     * Description: TODO 获取产品详情信息
     * Author: fwj
     */
    @Override
    public ResponseDTO getProductDetails() {
        return null;
    }
}
